/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.videodes.desktop;

import cz.muni.fi.pb138.videodes.entity.Genre;
import cz.muni.fi.pb138.videodes.entity.Movie;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JComponent;
import javax.swing.JList;
import javax.swing.TransferHandler;

/**
 *
 * @author vaclav
 */
public class MovieListTransferHandler extends TransferHandler {

    @Override
    protected Transferable createTransferable(JComponent c) {
        Movie movie = (Movie) ((JList) c).getSelectedValue();
        String transfer = movie.getId() + "\n"
                + movie.getName() + "\n"
                + movie.getDirector() + "\n"
                + movie.getYear() + "\n"
                + movie.getCountry() + "\n"
                + movie.getActors() + "\n"
                + movie.getGenres();
        return new StringSelection(transfer);
    }

    @Override
    public int getSourceActions(JComponent c) {
        return TransferHandler.MOVE;
    }

    @Override
    public boolean canImport(TransferHandler.TransferSupport info) {
        return true;
    }

    @Override
    public boolean importData(TransferHandler.TransferSupport info) {
        if (!info.isDrop()) {
            return false;
        }

        JList list = (JList) info.getComponent();
        MovieListModel listModel = (MovieListModel) list.getModel();

        // Get the string that is being dropped.
        Transferable t = info.getTransferable();
        String data;
        try {
            data = (String) t.getTransferData(DataFlavor.stringFlavor);
        } catch (Exception e) {
            return false;
        }

        String[] pieces = data.split("\n");

        Movie movie = new Movie();
        movie.setId(pieces[0]);
        movie.setName(pieces[1]);
        movie.setDirector(pieces[2]);
        if (!pieces[3].equals("null")) {
            movie.setYear(Integer.parseInt(pieces[3]));
        }
        movie.setCountry(pieces[4]);

        StringBuilder actors = new StringBuilder(pieces[5]);
        actors.deleteCharAt(0);
        actors.deleteCharAt(actors.lastIndexOf("]"));
        String[] actorsArray = actors.toString().split(",");
        Set<String> actorsSet = new HashSet<String>();
        
        for (String s : actorsArray) {
            actorsSet.add(s.trim());
        }
        movie.setActors(actorsSet);
        
        StringBuilder genres = new StringBuilder(pieces[6]);
        genres.deleteCharAt(0);
        genres.deleteCharAt(genres.lastIndexOf("]"));
        String[] genresArray = genres.toString().split(",");
        Set<Genre> genresSet = new HashSet<Genre>();
        
        for (String s : genresArray) {
            genresSet.add(Genre.valueOf(s.trim()));
        }
        movie.setGenres(genresSet);

        listModel.add(movie);

        return true;
    }

    @Override
    protected void exportDone(JComponent c, Transferable data, int action) {
        if (action == TransferHandler.MOVE) {
            JList source = (JList) c;
            MovieListModel model = (MovieListModel) source.getModel();
            model.remove((Movie) source.getSelectedValue());
        }
    }
}
